package cn.thoughtworks.school.requestParams;

import lombok.Getter;
import lombok.Setter;

import java.util.*;
import java.util.stream.Collectors;

@Setter
@Getter
public class YmlConvertQuiz {
    private List<Map> quizzes;
    private Set<String> tags;

    public YmlConvertQuiz() {
        this.quizzes = new ArrayList<>();
        this.tags = new HashSet<>();
    }

    public void add(Map data, String fileName) {
        List<String> tags = splitTags(fileName);
        this.tags.addAll(tags);
        if (Objects.nonNull(data.get("quiz"))) {
            List<Map> quizzes = (List<Map>) data.get("quiz");
            quizzes.forEach(quiz -> quiz.put("tags", tags));
            this.getQuizzes().addAll(quizzes);
        }
    }

    private List<String> splitTags(String fileName) {
        String[] tags = fileName.substring(fileName.indexOf("/") + 1, fileName.lastIndexOf(".")).split("/");

        return Arrays.asList(tags);
    }

    public List<Map> filterBasicQuizzes() {
        return this.quizzes.stream()
            .filter(item -> Objects.nonNull(item.get("answer")))
            .collect(Collectors.toList());
    }

    public List<Map> filterSurveyQuizzes() {
        return this.quizzes.stream()
            .filter(item -> Objects.isNull(item.get("answer")))
            .collect(Collectors.toList());
    }
}
